﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.Core.Metadata;
using Luna.WPF.ApplicationFramework;
using XWord.Fundamental.Domain.Model;
using Microsoft.Practices.ServiceLocation;
using XWord.Fundamental.Domain;
using System.Threading;
using XWord.WPF.ApplicationFramework;
using XWord.GUI.Presenters.Interface;

namespace XWord.GUI.Presenters.Fundamental
{
    [Singleton(typeof(WordTabPresenter))]
    public class WordTabPresenter : DockingPresenter
    {
        private IServiceLocator _serviceLocator;
        public WordTabPresenter(IServiceLocator entityFactory)
        {
            _serviceLocator = entityFactory;
            AlphabetList = BinaryFormatterHelper.Deserialize<IList<Alphabet>>(BinaryFormatterHelper.Path + "AlphabetList.data");
        }

        private IList<Alphabet> _alphabetList;
        public IList<Alphabet> AlphabetList
        {
            get { return _alphabetList ?? (_alphabetList = new List<Alphabet>()); }
            set
            {
                _alphabetList = value;
                NotifyOfPropertyChange("AlphabetList");
            }
        }

        public int SearchMode
        {
            get { return XWordAppConfig.Current.SearchTarget; }
            set
            {
                XWordAppConfig.Current.SearchTarget = value;
                NotifyOfPropertyChange("SearchMode");
                SearchEtyma(SearchText);
                
            }
        }

        private string _searchText;
        public string SearchText
        {
            get { return _searchText; }
            set
            {
                _searchText = value;
                NotifyOfPropertyChange("SearchText");
            }
        }

        private IWordExplorerPresenter _wordExplorerPresenter {get;set;}
        private IEtymaLibraryExplorerPresenter _etymaLibraryExplorerPresenter { get; set; }

        public void SearchEtyma(string text)
        {
            text = SearchText;
            if (string.IsNullOrEmpty(text))
                return;

            if (SearchMode > 0)
            {
                if (_wordExplorerPresenter == null)
                {
                    _wordExplorerPresenter = _serviceLocator.GetInstance<IWordExplorerPresenter>();
                }
                _wordExplorerPresenter.SearchWord(text);
                
                DockSitePresenter.Activate(_wordExplorerPresenter);
            }
            else
            {
                if (_etymaLibraryExplorerPresenter == null)
                {
                    _etymaLibraryExplorerPresenter = _serviceLocator.GetInstance<IEtymaLibraryExplorerPresenter>();
                }
                _etymaLibraryExplorerPresenter.Search(text.ToLower());
                DockSitePresenter.Activate(_etymaLibraryExplorerPresenter);
               
            }
        }

        public IDockSitePresenter DockSitePresenter { get; set; }

        public void SetSearchWord(object obj)
        {
            if (obj != null)
                SelectedAlphabet = obj as Alphabet;
            //if (text == null) return;
            //this.SearchText = text.ToString();
        }

        public void SaveAlphabet(Alphabet bet)
        {
            BinaryFormatterHelper.Serialize("AlphabetList.data", AlphabetList);
        }

        private Alphabet _selectedAlphabet;
        public Alphabet SelectedAlphabet
        {
            get { return _selectedAlphabet; }
            set
            {
                _selectedAlphabet = value;
                if (value != null)
                {
                    this.SearchText=value.Name;
                }
                NotifyOfPropertyChange("SelectedAlphabet");
            }
        }
    }
}
